package com.shanghaionstar.user.presenter;

import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.PrepaidCardResponse;
import com.shanghaionstar.user.iview.IPayCardView;
import com.shanghaionstar.user.service.PayCardService;
import com.shanghaionstar.utils.ApplicationUtil;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/7/29.
 */
public class PayCardPresenter extends BasePresenter<PrepaidCardResponse> {
    private Context        context;
    private IPayCardView   payCardView;
    private PayCardService service;
    private EditText       et_carnumber_value;
    private EditText       et_password_value;
    private Bundle bundle;

    public PayCardPresenter(IMVPScheduler mvpScheduler, Context context, EditText et_carnumber_value, EditText et_password_value,Bundle bundle) {
        super(mvpScheduler);
        this.context=context;
        this.et_carnumber_value=et_carnumber_value;
        this.et_password_value=et_password_value;
        this.bundle=bundle;
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        service=new PayCardService(context,et_carnumber_value,et_password_value,bundle);
        payCardView = (IPayCardView) view;
        setServiceProvider(service);
    }

    public void getData(){
        payCardView.setLoading(true);
        subscription=service.getObserval().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
               .subscribe(new Action1<PrepaidCardResponse>() {
                   @Override
                   public void call(PrepaidCardResponse prepaidCardResponse) {
                       if (!CommonUtil.isNull(prepaidCardResponse)&&!CommonUtil.isNull(prepaidCardResponse.getStatus())
                               && prepaidCardResponse.getStatus().equals("VALID")) {
                           payCardView.setLoading(false);
                            payCardView.showSuccess(prepaidCardResponse);
                       }else {
                           payCardView.setLoading(false);
                           Bundle bundle=new Bundle();
                           payCardView.showFailure(bundle);
                       }
                   }
               }, new Action1<Throwable>() {
                   @Override
                   public void call(Throwable throwable) {
                       view.setLoading(false);
//                       Bundle bundle=new Bundle();
//                       ErrorInfo errorInfo=new ErrorInfo();
//                       errorInfo.setCode(ErrorEnum.SB027_MSG007.getStringValue());
//                       errorInfo.setMsg("预付卡激活失败");
//                       bundle.putSerializable("errorInfo",errorInfo);
//                       payCardView.showError(bundle, (Exception) throwable);
                       payCardView.onResponseError((OnStarException) throwable);
                   }
               });
    }


    /**
     * @param @param  cardnumber
     * @param @param  password
     * @param @return 设定文件
     * @return boolean    返回类型
     * @throws
     * @Title: checkCardAndPassword
     * @Description: 检验卡号和密码格式是否正确
     */
    public boolean checkCardAndPassword(EditText cardnumber, EditText password) {
        if (CommonUtil.isNull(cardnumber.getText().toString())) {
            return false;
        }
        return !CommonUtil.isNull(password.getText().toString());
    }


    public boolean checkDataPPC(String ppcCardNum) {
        String card = ppcCardNum.trim();
        if (card.startsWith("D")) {
            return ApplicationUtil.getInstance().isGen10();
        } else {
            return true;
        }
    }
}
